package org.juggers.client.controller;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.List;

import org.juggers.client.JuggersMessages;
import org.juggers.client.ui.EventDialogBox;
import org.juggers.domain.AccessDeniedException;
import org.juggers.domain.Event;
import org.juggers.domain.ModelState;
import org.juggers.domain.NotConnectedException;
import org.juggers.remote.EventRemoteServiceAsync;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * Controller
 * 
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
@Singleton
public class EventController
{
    @Inject
    private ModelState model;

    @Inject
    private JuggersMessages messages;

    @Inject
    private EventRemoteServiceAsync eventService;

    @Inject
    private EventDialogBox dialogBox;

    public void showUpcomingEvents()
    {
        eventService.getUpcomingEvents( new AsyncCallback<List<Event>>()
        {

            @Override
            public void onSuccess( List<Event> result )
            {
                model.setEvents( result );
            }

            @Override
            public void onFailure( Throwable caught )
            {
                Window.alert( messages.error( caught.getMessage() ) );
            }
        } );
    }

    public void showCreateEvent()
    {
        if (model.getConnectedJugger() != null)
        {
            model.setEvent( new Event() );
            dialogBox.show();
            dialogBox.center();
        }
    }

    public void createEvent( Event event )
    {
        eventService.create( event, new AsyncCallback<Event>()
        {

            @Override
            public void onSuccess( Event result )
            {
                dialogBox.hide();
                Window.alert( "Evenement créé" );
            }

            @Override
            public void onFailure( Throwable caught )
            {
                if ( caught instanceof NotConnectedException )
                {
                    Window.alert( "Vous devez être connecté" );
                }
                else if (caught instanceof AccessDeniedException )
                {
                    Window.alert( "Vous n'êtes pas autorisé à faire cette action" );
                }
                else
                {
                    Window.alert( messages.error( caught.getMessage() ) );
                }
            }
        } );
    }

    public void cancelEdit()
    {
        dialogBox.hide();
    }

    public void showUpdateEvent( Event event )
    {
        if (model.getConnectedJugger() != null)
        {
            model.setEvent( event );
            dialogBox.show();
            dialogBox.center();
        }
    }

    public void update( Event event )
    {
        eventService.update( event, new AsyncCallback<Event>()
        {

            @Override
            public void onSuccess( Event result )
            {
                dialogBox.hide();
                Window.alert( "Evenement mis à jour" );
            }

            @Override
            public void onFailure( Throwable caught )
            {
                if ( caught instanceof NotConnectedException )
                {
                    Window.alert( "Vous devez être connecté" );
                }
                else if (caught instanceof AccessDeniedException )
                {
                    Window.alert( "Vous n'êtes pas autorisé à faire cette action" );
                }
                else
                {
                    Window.alert( messages.error( caught.getMessage() ) );
                }
            }
        } );
    }

    public void deleteEvent( Event event )
    {
        eventService.delete( event, new AsyncCallback<Void>()
        {

            @Override
            public void onSuccess( Void result )
            {
                Window.alert( "Evenement supprimé" );
            }

            @Override
            public void onFailure( Throwable caught )
            {
                Window.alert( messages.error( caught.getMessage() ) );
            }
        } );
    }

    public void subscribe( Event event )
    {
        eventService.subscribe( event, new AsyncCallback<Event>()
        {

            @Override
            public void onSuccess( Event result )
            {
                Window.alert( "Participation enregistrée" );
            }

            @Override
            public void onFailure( Throwable caught )
            {
                Window.alert( messages.error( caught.getMessage() ) );
            }
        } );
    }

    public void unsubscribe( Event event )
    {
        eventService.unsubscribe( event, new AsyncCallback<Event>()
        {

            @Override
            public void onSuccess( Event result )
            {
                Window.alert( "Participation annulée" );
            }

            @Override
            public void onFailure( Throwable caught )
            {
                Window.alert( messages.error( caught.getMessage() ) );
            }
        } );
    }
}
